<?php
/**
 * Created by PhpStorm.
 * User: angl
 * Date: 15/12/24
 * Time: 上午11:04
 */
namespace AuctionCore\Account\Back;

class Department
{

    private $id = 0;

    public function __construct($depart_id)
    {
        $this->id = $depart_id;
    }

    /**
     * 添加部门
     * @param $data
     * @return integer $last_insert_id
     */
    public static function create($data)
    {
        $data['status'] = 1;
        $data['createtime'] = date("Y-m-d H:i:s");
        return M('depart')->add($data);
    }

    /**
     * 设置部门负责人
     * @param $depart_id
     * @param $emp_id
     * @return bool
     */
    public function setLeader($depart_id, $emp_id)
    {
        //开启事务
        M()->startTrans();
        if(empty($depart_id) || empty($emp_id)){
            return false;
        }
        $map = array('emp_id' => $emp_id);
        $data = array(
            "is_head"=>1,
            "createtime"=>date('Y-m-d H:i:s')
        );
        $ok = M("employee")->where($map)->save($data);
        //这个部门下的其他的人id
        $map1 =array();
        $map1['depart_id'] = array('eq',$depart_id);
        $map1['emp_id'] = array('neq',$emp_id);
        $no_head_emp_info= M("employee")->field('emp_id')->where($map1)->select();
        $no_head_emp_id = array();
        foreach ($no_head_emp_info as $key => $value) {
            $no_head_emp_id[] = $value['emp_id'];
        }
        
        $data1 = array(
            "is_head"=>0,
            "createtime"=>date('Y-m-d H:i:s')
        );
        $map2 = array();
        if ($no_head_emp_id) {
            $map2['emp_id'] = array('in',$no_head_emp_id);
            $ok1 = M('employee')->where($map2)->save($data1);
        }  
        //出错回滚
        if ($ok < 1 || ($ok1 < 1 && $no_head_emp_id)) {

            M()->rollback();

            return false;
        }
        M()->commit();

        return true;
    }

    /**
     * 获取部门详细信息
     * @return bool|mixed
     */
    public function detail()
    {
        if($this->id<1){
            return false;
        }
        return M('depart')->where("id={$this->id}")->find();
    }

    /**
     * 通过名称获取部门
     * @param $name
     * @param int $pid
     * @return mixed
     */
    public static function getDetailByName($name,$pid=0)
    {
        $map = ['name' => $name];
        if($pid>0){
            $map['pid'] = $pid;
        }
        return M('depart')->where($map)->find();
    }

    /**
     * 获取所有部门列表
     * @param string $cols
     * @return mixed
     */
    public static function getList($cols="*")
    {
        return  M('depart')->field($cols)->order('is_branch_department')->select();
    }

    /**
     * 获取下属列表
     * @param string $cols
     * @return array
     */
    public function getChildent($cols="*")
    {
        $map = array();
        $map['pid'] = $this->id;
        return M('depart')->field($cols)->where($map)->select();
    }


     private static function get_childen($id)
    {
        $map = array();
        $map['pid'] = $id;
        return M('depart')->field('id')->where($map)->select();
    }
       /**
     * 获取指定部门id下所有部门
     * @param string $id
     * @return array
     */
    public static function get_all_childen($id)
    {   
        static $childen_id = array();
        $is_childen = self::get_childen($id);
        if (!empty($is_childen)) {
            foreach ($is_childen as $key => $value) {
                $childen_id[] = $value['id'];
                self::get_all_childen($value['id']);
            }
        }else{
            $childen_id[] = $id;
        }
        return array_unique($childen_id);
    }
    /**
    * 更新指定部门数据 以及部门下所有子部门的字段
    *
    **/
    public static function updateAll($id,$data,$children_id){
        //开启事务
        M()->startTrans();
        if($id <1){
            return false;
        }
        //本条数据的修改
        $ok = M('depart')->where(array('id'=>$id))->save($data);
        if ($data['is_branch_department'] == 1) {
            $new_data = array(
                'is_branch_department'=> 1,
                'branch_id'=>$data['branch_id'],
                'createtime'=>date('Y-m-d H:i:s')
                );
        }else{
            $new_data = array(
                'is_branch_department'=> 0,
                'branch_id' => 0,
                'createtime'=>date('Y-m-d H:i:s')
                );
        }
        // 修改部门下所有子部门的 is_branch_department branch_id
        $ok1 = 1;
        $map = array();
        if (!empty($children_id)) {
            $map['id'] = array('in',$children_id);
            $ok1 = M('depart')->where($map)->save($new_data);
        }
        //出错回滚
        if ($ok < 1 || $ok1 < 1 ) {

            M()->rollback();

            return false;
        }
        M()->commit();

        return true;
    }

    /**
    * 通过子部门递归找到截止到指定父id的中间所有pid id name 信息
    * @param array $all_depart,string $branch_id
    * @return array
    */
    public static function get_father_depart($all_depart,$branch_id,$new_arr = array()){
        $pids = array();
        foreach ($all_depart as $key => $value) {
            if ($value == $branch_id) {
                continue;
            }else {
                $info = M('depart')->field('id,pid,name')->where(array('id'=>$value))->find();
                $new_arr[] = $info;
                if ($info['pid'] != $branch_id) {
                    $pids[] = $info['pid'];
                } 
            }
        }
        if (!empty($pids)) {
            $new_arr = self::get_father_depart($pids,$branch_id,$new_arr);
        }
        assoc_unique($new_arr,'id');
        return  $new_arr;
    }
    /**
     * 更新部门信息
     * @param $data
     * @return bool
     */
    public function updateDepart($data)
    {
        if($this->id<1){
            return false;
        }
        return M('depart')->where("id={$this->id}")->save($data);
    }

    /**
     * 删除部门
     * @return bool
     */
    public function delete()
    {
        if($this->id<1){
            return false;
        }
        $map['id'] = $this->id;
        return M('depart')->where($map)->delete();

    }

    /**
     * 通过id获取部门信息
     * @param $id
     * @return bool|mixed
     */
    public static function getDepartById($id)
    {
        if($id<1) return false;
        return M('depart')->where("id={$id}")->find();
    }

    /**
     * 获取部门负责人
     * @return bool|mixed
     */
    public function getDepartHead()
    {
        if($this->id<1){
            return false;
        }
        $map = array(
            'depart_id' => $this->id,
            'is_head' => 1
        );
        return M('employee')->where($map)->find();
    }

        /**
     * 获取部门所有人
     * @return bool|mixed
     */
    public function getDepartPeople()
    {
        if($this->id<1){
            return false;
        }
        $map = array(
            'depart_id' => $this->id,
        );
        return M('employee')->field('emp_id,real_name,is_head')->where($map)->select();
    }

    /**
     *获取部门成员
     */
    public function getDepartMember(){
        if($this->id<1){
            return false;
        }
        $map = [
            'depart_id'=>$this->id,
            'status' => 0, //启用的账号20160930产品需求,立字为证
        ];
        $list = M('employee')->field("emp_id,depart_id,mobile,real_name,is_head")->where($map)->select();
        return $list;
    }
    
    /**
     * 获取分公司列表
     */
    public static function getBranchCompanyList()
    {
       return  M('depart')->where('id=branch_id')->select();
        
    }

    /**
     * 根据depart_id获取分公司
     * @param int $depart_id 部门ID
     * @return mixed
     */
    public static function getBranchCompanyByDepartId($depart_id)
    {
        $departModel = M('depart');
        $branch_id = $departModel->where(array('id'=>$depart_id))->getField('branch_id');
        return $departModel->where(['id'=>$branch_id])->getField('name');
    }
    /**
    * 获取父级对应branch_id
    */

    public static function getPidBranchId($pid){
        return  M('depart')->field('branch_id')->where(array('id'=>$pid))->find();
    }


}